﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Locations
{
    public partial class AddBuilding : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["AddBuildingLabeltext"] != null && IsPostBack)
            {
                AddBuildingLabel.Text = (string)HttpContext.Current.Session["AddBuildingLabeltext"];
                //OKButton.Enabled = false;
            }
            else if (IsPostBack)
            {
                AddBuildingLabel.Text = "";
            }
            else
            {
                AddBuildingLabel.Text = "";
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton locationsMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("LocationsMenuButton");
            locationsMenuButton.CssClass = "selected";

            if (AddBuildingLabel.Text != "") return;
            if (HttpContext.Current.Session["CurrentDepartmentId"] != null)
            {
                int CurrentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                cs499.Database.Department CurrentDepartment = ListOfDepartment.GetDepartment(ID: CurrentDepartmentId);
                AddBuildingLabel.Text = "Input information for new building in The " + CurrentDepartment.Name + " department";
            }
            else
            {
                Response.Redirect("~/Locations/ListLocations.aspx");
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Locations/ListLocations.aspx");
        }

        protected void OKButton_Click(object sender, EventArgs e)
        {
            String buildingname = inputbuildingname.Text;
            String buildingcode = inputbuildingcode.Text;
            if (buildingname=="")
            {
                String errmsg = "Building Name cannot be blank";
                if (AddBuildingLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddBuildingLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddBuilding.aspx", true);
                }
                return;
            }
            if (buildingcode == "")
            {
                String errmsg = "Building Code cannot be blank";
                if (AddBuildingLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddBuildingLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddBuilding.aspx", true);
                }
                return;
            }
            Department curdep;
            if (HttpContext.Current.Session["CurrentDepartmentId"] == null)
            {
                DropDownList seldep = (DropDownList)Page.Master.FindControl("LoginViewForDepartmentSelection_DepartmentSelection");

                curdep = ListOfDepartment.GetDepartment(Name: seldep.Text);
                if (curdep == null) HttpContext.Current.Session["CurrentDepartmentId"] = null;
                else HttpContext.Current.Session["CurrentDepartmentId"] = curdep.Id;
                HttpContext.Current.Session["CurrentDepartmentselectedindex"] = seldep.SelectedIndex;
            }
            else
            {
                int idtocheckfor = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                curdep = ListOfDepartment.GetDepartment(ID: idtocheckfor);
            }

            if(curdep.GetBuilding(buildingname, buildingcode)!=null)
            {
                String errmsg = "Building already exists in this department";
                if (AddBuildingLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddBuildingLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddBuilding.aspx", true);
                }
                return;
            }

            if (curdep.CreateBuilding(buildingname, buildingcode) == null)
            {
                String errmsg = "Unknown Error while creating building";
                if (AddBuildingLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddBuildingLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddBuilding.aspx", true);
                }
                return;
            }
            HttpContext.Current.Session["AddBuildingLabeltext"] = "";
            AddBuildingLabel.Text = "Success";
            Response.Redirect("~/Locations/ListLocations.aspx?action=success");
        }
    }
}